package com.offcn.com.stack;

import java.util.Stack;

/*
用一个栈实现另一个栈的排序
 */
public class SortStack {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.add(12);
        stack.add(23);
        stack.add(13);
        stack.add(59);
        stack.add(34);

        System.out.println(stack);
        sortStack(stack);
        System.out.println(stack);
    }

    private static void sortStack(Stack<Integer> stack) {
        //创建一个辅助栈
        Stack<Integer> help = new Stack<>();
        while ( !stack.isEmpty()) {
            int cur = stack.pop();

            while (!help.isEmpty() && help.peek() < cur) {
                stack.push(help.pop());
            }
            help.push(cur);
        }

        while (!help.isEmpty()) {
            stack.push(help.pop());
        }
    }

}
